package com.df.barcodescaning.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.df.barcodescaning.MyApplication;
import com.df.barcodescaning.R;
import com.df.barcodescaning.bean.UserData;
import com.df.barcodescaning.bean.UserGroup;
import com.df.barcodescaning.widget.SwipeItemLayout;

import java.util.Map;

/**
 * Created by G on 2018/5/31.
 * 用户管理界面
 */
public class UserManageActivity extends BaseActivity {

    public static final String KEY_SERIAL_USER_GROUP = "usergroup_data";
    public static final int CODE_REQUEST_ADD = 6604;

    private UserGroup userGroup;
    private Map<String, UserData> userMap;
//    private List<String> userList;

    private UserManageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_user_manager);
        setTitle(R.string.app_name);
        super.isNeedBackKey = true;
        super.onCreate(savedInstanceState);
        setTitle(R.string.page_tittle_usermanager);

        userGroup = (UserGroup) getIntent().getSerializableExtra(KEY_SERIAL_USER_GROUP);
        userMap = userGroup.getValues();

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_id);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        adapter = new UserManageAdapter(this, userGroup);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_usermanager, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.options_manager:
//                Toast.makeText(this, "options_manager", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this, AddUserActivity.class);
                intent.putExtra(KEY_SERIAL_USER_GROUP, userGroup);
                startActivityForResult(intent, CODE_REQUEST_ADD);
                break;
            case R.id.options_logout:
//                MyApplication.getInstance().deleteLoggedUser();
//                startActivity(new Intent(this, LoginActivity.class));
//                finish();
                onBackPressed();
                break;
        }
//        return super.onOptionsItemSelected(item);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_REQUEST_ADD && data != null) {
            String newUserName = data.getStringExtra("username");
            String newPassword = data.getStringExtra("password");
            UserData user = new UserData();
            user.setUserName(newUserName);
            user.setUserPsw(newPassword);
            userMap.put(newUserName, user);
            adapter.addUser(newUserName);
        }
    }

    @Override
    public void onBackPressed() {
        userGroup.setValues(userMap);
        MyApplication.getInstance().saveRegistedUser(userGroup);
        super.onBackPressed();
    }
}
